import java.util.ArrayList;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode :
 * @author: WXY
 * @create: 2023-01-01 10:47
 * @Version 1.0
 **/
public class offer_fiveteen1_largestValues {
    List<Integer> list = new ArrayList<>();

    public List<Integer> largestValues(TreeNode root) {
        dfs(root, 0);
        return list;
    }

    private void dfs(TreeNode root, int depth) {
        if (root != null) {
            if (list.size() <= depth) list.add(Integer.MIN_VALUE);
            int max = list.get(depth);
            list.set(depth, Math.max(max, root.val));
            dfs(root.left, depth + 1);
            dfs(root.right, depth + 1);
        }
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
